import java.util.*;

public class HesuanJianCe {
    static void dfs(int n, int i, int[] checkList, int[][] mat, int[] ans) {
        // n行列，第i个人，检测列表，会面列表，ans
        ans[0]++;
        checkList[i] = 1;
        for (int j = 0; j < n; j++) {
            if (j != i && mat[i][j] == 1 && checkList[j] == 0) {
                dfs(n, j, checkList, mat, ans);
            }
        }
    }

    public static void main(String[] args) {
        maindfs();
    }

    private static void maindfs() {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine(); // Consume newline
        String startListStr = scanner.nextLine();
        StringTokenizer tokenizer = new StringTokenizer(startListStr, ",");
        List<Integer> startList = new ArrayList<>();//有几个先中招的
        while (tokenizer.hasMoreTokens()) {
            startList.add(Integer.parseInt(tokenizer.nextToken()));
        }
        int[][] mat = new int[n][n];//初始化矩阵
        for (int i = 0; i < n; i++) {
            String row = scanner.nextLine();
            tokenizer = new StringTokenizer(row, ",");
            for (int j = 0; j < n; j++) {
                mat[i][j] = Integer.parseInt(tokenizer.nextToken());
            }
        }
        int[] checkList = new int[n];
        int[] ans = {0};
        for (int i : startList) {//从那些人开始中招开始
            if (checkList[i] == 0) {
                // n行列，第i个人，检测列表，会面列表，ans
                dfs(n, i, checkList, mat, ans);
            }
        }
        System.out.println(ans[0] - startList.size());
    }


    public static void mainbfs(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine(); // Consume newline
        String[] startListStr = scanner.nextLine().split(",");
        int[] startList = new int[startListStr.length];
        for (int i = 0; i < startListStr.length; i++) {
            startList[i] = Integer.parseInt(startListStr[i]);
        }
        int[][] mat = new int[n][n];
        for (int i = 0; i < n; i++) {
            String[] row = scanner.nextLine().split(",");
            for (int j = 0; j < n; j++) {
                mat[i][j] = Integer.parseInt(row[j]);
            }
        }
        int[] checkList = new int[n];
        for (int i : startList) {
            checkList[i] = 1;
        }
        int ans = 0;
        Queue<Integer> queue = new ArrayDeque<>();
        for (int i : startList) {
            queue.offer(i);
        }
        while (!queue.isEmpty()) {
            int i = queue.poll();
            ans++;
            for (int j = 0; j < n; j++) {
                if (j != i && mat[i][j] == 1 && checkList[j] == 0) {
                    checkList[j] = 1;
                    queue.offer(j);
                }
            }
        }
        System.out.println(ans - startList.length);
    }
}
